package com.flowershop.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flowershop.entity.Employee;
import com.flowershop.service.EmployeeService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/employees")
@CrossOrigin
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginForm) {
        String username = loginForm.get("username");
        String password = loginForm.get("password");
        
        log.info("收到登录请求 - 用户名: {}, 密码: {}", username, password);
        
        Employee employee = employeeService.login(username, password);
        
        if (employee != null) {
            // 生成简单的token
            String token = username + "_" + System.currentTimeMillis();
            
            Map<String, Object> result = Map.of(
                "token", token,
                "id", employee.getId(),
                "username", employee.getUsername(),
                "name", employee.getName(),
                "role", employee.getRole()
            );
            log.info("登录成功 - 用户名: {}", username);
            return result;
        }
        
        log.error("登录失败 - 用户名: {}", username);
        throw new RuntimeException("用户名或密码错误");
    }

    @GetMapping("/page")
    public Page<Employee> page(@RequestParam(defaultValue = "1") Integer current,
                             @RequestParam(defaultValue = "10") Integer size,
                             @RequestParam(required = false) String name) {
        Page<Employee> page = new Page<>(current, size);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        if (name != null && !name.isEmpty()) {
            queryWrapper.like(Employee::getName, name);
        }
        return employeeService.page(page, queryWrapper);
    }

    @GetMapping("/{id}")
    public Employee getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        employee.setPassword(null);
        return employee;
    }

    @PostMapping
    public Employee save(@RequestBody Employee employee) {
        employeeService.save(employee);
        employee.setPassword(null);
        return employee;
    }

    @PutMapping
    public Employee update(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        employee.setPassword(null);
        return employee;
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return employeeService.removeById(id);
    }
} 